package cn.lyq.card.ui.web

import android.content.Context
import android.content.Intent
import android.view.View
import android.webkit.WebView
import android.widget.LinearLayout
import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.OrderConfirmInfo
import cn.lyq.card.data.model.OrderDetailInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.ui.order.OrderPayFailedActivity
import cn.lyq.card.ui.order.OrderPaySuccessActivity
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ToastUtils
import com.just.agentweb.AgentWeb
import com.just.agentweb.WebChromeClient
import kotlinx.android.synthetic.main.act_web.*
import kotlinx.android.synthetic.main.layout_title.*

class WebPayActivity : BaseActivity() {

    companion object {
        private const val KEY_INFO = "INFO"

        @JvmStatic
        fun start(context: Context, info: OrderConfirmInfo?) {
            val starter = Intent(context, WebPayActivity::class.java)
                .putExtra(KEY_INFO, info)
            context.startActivity(starter)
        }
    }

    private val mModel by lazy {
        getVM(PayVM::class.java)
    }
    private val mInfo by lazy {
        intent.getSerializableExtra(KEY_INFO)
            ?.takeIf { it is OrderConfirmInfo }
            ?.run { this as OrderConfirmInfo }
    }
    private lateinit var mAgentWeb: AgentWeb
    private var isFirstIn = true

    override fun getLayoutId(): Int {
        return R.layout.act_web
    }

    override fun initView() {
        super.initView()
        tvTitleSplit.visibility = View.GONE
        val webChromeClient = object : WebChromeClient() {
            override fun onReceivedTitle(view: WebView?, title: String?) {
                super.onReceivedTitle(view, title)
                if (title != null && title.isNotEmpty()) {
                    setMyTitle(title)
                }
            }
        }
        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(groupWeb, LinearLayout.LayoutParams(-1, -1))
            .useDefaultIndicator()
            .setWebChromeClient(webChromeClient)
            .createAgentWeb()
            .ready()
            .go(mInfo?.payUrl)
    }

    override fun onResume() {
        super.onResume()
        if (isFirstIn) {
            isFirstIn = false
        } else {
            getPayResult()
        }
    }

    private fun getPayResult() {
        mModel.getOrderDetail(mInfo?.orderId ?: "").observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> showLoading()
                Status.SUCCESS -> handlePayResult(it.requireData())
                Status.ERROR -> {
                    ToastUtils.showShort(it.message.getMsg())
                    dismissLoading()
                }
            }
        })
    }

    private fun handlePayResult(info: OrderDetailInfo) {
        when (info.payStatus) {
            1 -> {
                OrderPaySuccessActivity.start(this, info)
                finish()
            }
            2 -> {
                OrderPayFailedActivity.start(this, mInfo)
                finish()
            }
        }
        dismissLoading()
    }

    override fun initListener() {
        super.initListener()
        imgBack.setClick {
            finish()
        }
    }

    override fun onBackPressed() {
        if (!mAgentWeb.back()) {
            super.onBackPressed()
        }
    }
}